package com.example.onlinemusic.service.impl;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.service.LoveMusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class LoveMusicServiceImpl implements LoveMusicService {

    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @Override
    public Boolean addLoveMusic(int userId, int musicId) {
        // 先判断当前音乐是否已经收藏了
        Music loveMusic = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId, musicId);
        if (loveMusic != null) {
            return false;
        }

        return loveMusicMapper.addLoveMusic(userId, musicId);
    }


    @Override
    public Boolean cancelLoveMusic(int userId, int musicId) {
        int i = loveMusicMapper.cancelLoveMusic(userId, musicId);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean cancelLoveMusicByMusicId(int musicId) {
        int i = loveMusicMapper.cancelLoveMusicByMusicId(musicId);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Music> findLoveMusicByTitleAndUID(int userId, String title) {
        return loveMusicMapper.findLoveMusicByTitleAndUID(userId, title);
    }

    @Override
    public List<Music> findAllLoveMusic(int userId) {
        return loveMusicMapper.findAllLoveMusic(userId);
    }

    @Override
    public Boolean findLoveMusicByMusicId(int musicId) {
        Integer userId = loveMusicMapper.findLoveMusicByMusicId(musicId);
        if (userId != null) {
            return true;
        }
        return false;
    }
}
